<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/

  require('includes/application_top.php');

  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');

  if (tep_not_null($action)) {
    switch ($action) {
      case 'delete_confirm':
        if (isset($HTTP_POST_VARS['pages_hours_id'])) {
		  $pages_hours_id = tep_db_prepare_input($HTTP_POST_VARS['pages_hours_id']);
		  tep_db_query("delete from " . TABLE_PAGES_HOURS . " where pages_hours_id = '" . (int)$pages_hours_id . "'");
		  tep_db_query("delete from " . TABLE_PAGES_HOURS_DESCRIPTION . " where pages_hours_id = '" . (int)$pages_hours_id . "'");
		  
		  tep_redirect(tep_href_link(FILENAME_PAGES_HOURS));
		}
		break;
	  case 'insert':
      case 'update':
        if (isset($HTTP_POST_VARS['pages_hours_id'])) $pages_hours_id = tep_db_prepare_input($HTTP_POST_VARS['pages_hours_id']);
		
		$sql_data_array = array('sort_order' => tep_db_prepare_input($HTTP_POST_VARS['sort_order']));
		
        if ($action == 'insert') {
          tep_db_perform(TABLE_PAGES_HOURS, $sql_data_array);

          $pages_hours_id = tep_db_insert_id();
        } elseif ($action == 'update') {
		  tep_db_perform(TABLE_PAGES_HOURS, $sql_data_array, 'update', "pages_hours_id = '" . (int)$pages_hours_id . "'");
        }

        $languages = tep_get_languages();
		$title_array = $HTTP_POST_VARS['title'];
		$time_1_array = $HTTP_POST_VARS['time_1'];
		$time_2_array = $HTTP_POST_VARS['time_2'];
        for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
          $language_id = $languages[$i]['id'];

          $sql_data_array = array('title' => tep_db_prepare_input($title_array[$language_id]),
								  'time_1' => tep_db_prepare_input($time_1_array[$language_id]),
								  'time_2' => tep_db_prepare_input($time_2_array[$language_id]));

          if ($action == 'insert') {
            $insert_sql_data = array('pages_hours_id' => $pages_hours_id,
                                     'language_id' => $languages[$i]['id']);

            $sql_data_array = array_merge($sql_data_array, $insert_sql_data);

            tep_db_perform(TABLE_PAGES_HOURS_DESCRIPTION, $sql_data_array);
          } elseif ($action == 'update') {
            tep_db_perform(TABLE_PAGES_HOURS_DESCRIPTION, $sql_data_array, 'update', "pages_hours_id = '" . (int)$pages_hours_id . "' and language_id = '" . (int)$languages[$i]['id'] . "'");
          }
        }

        /*if (USE_CACHE == 'true') {
          tep_reset_cache_block('categories');
          tep_reset_cache_block('also_purchased');
        }*/

        tep_redirect(tep_href_link(FILENAME_PAGES_HOURS, 'hID=' . $pages_hours_id));
        break;
    }
  }

  $hID = (isset($HTTP_GET_VARS['hID'])) ? $HTTP_GET_VARS['hID'] : 1;

  $pages_hours_query = tep_db_query("select phd.title, phd.time_1, phd.time_2, ph.sort_order from " . TABLE_PAGES_HOURS . " ph inner join " . TABLE_PAGES_HOURS_DESCRIPTION . " phd on (ph.pages_hours_id = phd.pages_hours_id) where ph.pages_hours_id = '" . $hID . "' and language_id = '" . (int)$languages_id . "'");
  $pages_hours = tep_db_fetch_array($pages_hours_query);

  require(DIR_WS_INCLUDES . 'template_top.php');
?>

    <table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_NAME; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_TIME_1; ?></td>
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_TIME_2; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_SORT_ORDER; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
              </tr>
<?php
  $pages_hours_query = tep_db_query("select phd.pages_hours_id, phd.title, phd.time_1, phd.time_2, ph.sort_order from " . TABLE_PAGES_HOURS . " ph inner join " . TABLE_PAGES_HOURS_DESCRIPTION . " phd on (ph.pages_hours_id = phd.pages_hours_id) where language_id = '" . (int)$languages_id . "' order by ph.sort_order, pages_hours_id");
  while ($pages_hours = tep_db_fetch_array($pages_hours_query)) {
    if ((!isset($HTTP_GET_VARS['hID']) || (isset($HTTP_GET_VARS['hID']) && ($HTTP_GET_VARS['hID'] == $pages_hours['pages_hours_id']))) && !isset($hInfo) && (substr($action, 0, 3) != 'new')) {
      $ph_extra_query = tep_db_query("select title, time_1, time_2 from " . TABLE_PAGES_HOURS_DESCRIPTION . " where pages_hours_id = '" . (int)$pages_hours['pages_hours_id'] . "'");
      $ph_extra = tep_db_fetch_array($ph_extra_query);

      //$cInfo_array = array_merge($configuration, $cfg_extra);
      $hInfo = new objectInfo($pages_hours);
    }

    if ( (isset($hInfo) && is_object($hInfo)) && ($pages_hours['pages_hours_id'] == $hInfo->pages_hours_id) ) {
      echo '                  <tr id="defaultSelected" class="dataTableRowSelected" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link(FILENAME_PAGES_HOURS, 'hID=' . $hInfo->pages_hours_id . '&action=edit') . '\'">' . "\n";
    } else {
      echo '                  <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link(FILENAME_PAGES_HOURS, 'hID=' . $pages_hours['pages_hours_id']) . '\'">' . "\n";
    }
?>
                <td class="dataTableContent"><?php echo $pages_hours['title']; ?></td>
                <td class="dataTableContent"><?php echo $pages_hours['time_1']; ?></td>
                <td class="dataTableContent"><?php echo $pages_hours['time_2']; ?></td>
                <td class="dataTableContent" align="right"><?php echo $pages_hours['sort_order']; ?></td>
                <td class="dataTableContent" align="right"><?php if ( (isset($hInfo) && is_object($hInfo)) && ($pages_hours['pages_hours_id'] == $hInfo->pages_hours_id) ) { echo tep_image(DIR_WS_images . 'icon_arrow_right.gif', ''); } else { echo '<a href="' . tep_href_link(FILENAME_PAGES_HOURS, 'hID=' . $pages_hours['pages_hours_id']) . '">' . tep_image(DIR_WS_images . 'icon_info.gif', IMAGE_ICON_INFO) . '</a>'; } ?>&nbsp;</td>
              </tr>
<?php
  }
?>
              <tr>
                <td colspan="5"><table border="0" width="100%" cellspacing="0" cellpadding="2">
                  <tr>
                    <td align="right" class="smallText"><?php echo tep_draw_button(IMAGE_NEW_HOUR, 'plus', tep_href_link(FILENAME_PAGES_HOURS, 'hID=' . $hInfo->pages_hours_id . '&action=new')); ?>&nbsp;</td>
                  </tr>
                </table></td>
              </tr>
            </table></td>
<?php
  $heading = array();
  $contents = array();

  switch ($action) {
    case 'new':
      $heading[] = array('text' => '<strong>' . TEXT_INFO_HEADING_NEW . '</strong>');

	  $contents = array('form' => tep_draw_form('pages_hours', FILENAME_PAGES_HOURS, 'action=insert', 'post'));
	  $contents[] = array('text' => TEXT_NEW_INTRO);

	  $category_inputs_string = '';
	  $languages = tep_get_languages();
	  $category_inputs_string .= '<div style="line-height:22px">';
	  for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
		$category_inputs_string .= '<br />' . tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']) . '&nbsp;' . tep_draw_input_field('title[' . $languages[$i]['id'] . ']');
		$category_inputs_string .= '<span style="padding-left:28px">' . tep_draw_input_field('time_1[' . $languages[$i]['id'] . ']') . '</span>';
		$category_inputs_string .= '<span style="padding-left:28px">' . tep_draw_input_field('time_2[' . $languages[$i]['id'] . ']') . '</span><br />';
	  }
	  $category_inputs_string .= '<br />' . TEXT_SORT_ORDER;
	  $category_inputs_string .= '<br />' . tep_draw_input_field('sort_order', '0', 'size="4"');
	  $category_inputs_string .= '</div>';

	  $contents[] = array('text' => '<br />' . $category_inputs_string);
	  $contents[] = array('align' => 'center', 'text' => '<br />' . tep_draw_button(IMAGE_SAVE, 'disk', null, 'primary') . tep_draw_button(IMAGE_CANCEL, 'close', tep_href_link(FILENAME_PAGES_HOURS, 'hID=' . (int)$HTTP_GET_VARS['hID'])));
      break;
    case 'edit':
      $heading[] = array('text' => '<strong>' . TEXT_INFO_HEADING_EDIT . '</strong>');

	  $contents = array('form' => tep_draw_form('pages_hours', FILENAME_PAGES_HOURS, 'action=update', 'post') . tep_draw_hidden_field('pages_hours_id', $hInfo->pages_hours_id));
	  $contents[] = array('text' => TEXT_EDIT_INTRO);

	  $category_inputs_string = '';
	  $languages = tep_get_languages();
	  $category_inputs_string .= '<div style="line-height:22px">';
	  for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
		$pages_hours_array = tep_get_pages_hours_content($hInfo->pages_hours_id, $languages[$i]['id']);
		$category_inputs_string .= '<br />' . tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']) . '&nbsp;' . tep_draw_input_field('title[' . $languages[$i]['id'] . ']', $pages_hours_array['title']);
		$category_inputs_string .= '<span style="padding-left:28px">' . tep_draw_input_field('time_1[' . $languages[$i]['id'] . ']', $pages_hours_array['time_1']) . '</span>';
		$category_inputs_string .= '<span style="padding-left:28px">' . tep_draw_input_field('time_2[' . $languages[$i]['id'] . ']', $pages_hours_array['time_2']) . '</span><br />';
	  }
	  $category_inputs_string .= '<br />' . TEXT_SORT_ORDER;
	  $category_inputs_string .= '<br />' . tep_draw_input_field('sort_order', $hInfo->sort_order, 'size="4"');
	  $category_inputs_string .= '</div>';

	  $contents[] = array('text' => '<br />' . $category_inputs_string);
	  $contents[] = array('align' => 'center', 'text' => '<br />' . tep_draw_button(IMAGE_SAVE, 'disk', null, 'primary') . tep_draw_button(IMAGE_CANCEL, 'close', tep_href_link(FILENAME_PAGES_HOURS, 'hID=' . $hInfo->pages_hours_id)));
      break;
    case 'delete':
        $heading[] = array('text' => '<strong>' . TEXT_INFO_HEADING_DELETE . '</strong>');

        $contents = array('form' => tep_draw_form('pages_hours', FILENAME_PAGES_HOURS, 'action=delete_confirm') . tep_draw_hidden_field('pages_hours_id', $hInfo->pages_hours_id));
        $contents[] = array('text' => TEXT_DELETE_INTRO);
        $contents[] = array('text' => '<br /><strong>' . $hInfo->title . '</strong>');
        $contents[] = array('align' => 'center', 'text' => '<br />' . tep_draw_button(IMAGE_DELETE, 'trash', null, 'primary') . tep_draw_button(IMAGE_CANCEL, 'close', tep_href_link(FILENAME_PAGES_HOURS, 'hID=' . $hInfo->pages_hours_id)));
		break;
	default:
      if (isset($hInfo) && is_object($hInfo)) {
        $heading[] = array('text' => '<strong>&nbsp;' . $hInfo->title . '</strong>');

        $contents[] = array('align' => 'center', 'text' => tep_draw_button(IMAGE_EDIT, 'document', tep_href_link(FILENAME_PAGES_HOURS, 'hID=' . $hInfo->pages_hours_id . '&action=edit')) . tep_draw_button(IMAGE_DELETE, 'trash', tep_href_link(FILENAME_PAGES_HOURS, 'hID=' . $hInfo->pages_hours_id . '&action=delete')));
      }
      break;
  }

  if ( (tep_not_null($heading)) && (tep_not_null($contents)) ) {
    echo '            <td width="25%" valign="top">' . "\n";

    $box = new box;
    echo $box->infoBox($heading, $contents);

    echo '            </td>' . "\n";
  }
?>
          </tr>
        </table></td>
      </tr>
    </table>

<?php
  require(DIR_WS_INCLUDES . 'template_bottom.php');
  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
